$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_ydczjhwcb_m")
    const dgrid1 = $NG.getCmpApi("p_form_ydczjhwcb_d1")
    const getHeadValue = (str) => mstform.getItem(str).getValue()

    if(page.oType == 'add'){
        getToMonth()
        mstform.getItem("u_nd").setValue({
            label: new Date().getFullYear(),
            value: new Date().getFullYear()
        })
    }

    useValuesChange(async ()=>{
        await getYearInfo()
        getToMonth()
    },"p_form_ydczjhwcb_m.u_htmc")

        useValuesChange(()=>{
        getToMonth()
    },"p_form_ydczjhwcb_m.u_yd")

    useUpdateRows(({args}) => {
        console.log(`args`,args[0]);
        const rows = args[0]
        rows.forEach(item=>{
            item.u_njh = window._catch_amt
        })
        sumAmt(args[0])
    }, "p_form_ydczjhwcb_d1")

    useBeforeClick(() => {
        const u_yd = mstform.getItem("u_yd").getValue()
        if (!u_yd) {
            updateRowslock = true
            $NG.alert("请先填写表头月度字段！")
            return false
        }
    }, "addrow")


    // 获取月度产值计划完成表的年收入
    async function getYearInfo(){
        const phid = mstform.getItem("u_htmc").getValue()
        if(!phid) return 
        await $NG.execServer("SELECT",{sql:`select u_bnczjh from p_form_ydczjhwcb_m where phid = '${phid}'`},(res)=>{
            if(res.count == 0)return 
            window._catch_amt = JSON.parse(res.data)[0].extendObjects
        })
    }

        // 拉取上个月的金额
    async function getToMonth(){
        let phid = mstform.getItem("u_yd").getValue()
        let cnt = mstform.getItem("u_htmc").getValue()
        if(!phid || !cnt) return 
        phid = (Number(phid) || 0)
        if(phid != 6870000000000553){
            phid--
        } else {
            dgrid1.clearRows()
            return 
        }
        await $NG.execServer("SELECT",{sql:`select d.u_xyczjh from p_form_ydczjhwcb_m m left join p_form_ydczjhwcb_d1 d on d.pphid= m.phid where m.u_yd = '${phid}' and m.u_htmc = '${cnt}'`},(res)=>{
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            dgrid1.clearRows().then(()=>{
                dgrid1.addRows({
                u_yjh:datas.u_xyczjh
            })
            })
        })
    }

    function sumAmt(rows){
        rows.forEach(el=>{
            // el.u_njhwcbl = (Number(el.u_njh) || 0) == 0?0:(Number((Number(el.u_zncljwc) || 0) / (Number(el.u_njh) || 0)) || 0)
        })
        dgrid1.refreshView()
    }
})